Routing
Instead of adding pages imperatively, functions can get registered for a nav path.
nav.route is a NavRouter bound to the nav,
Router collects routes without a nav (for example in a module which
gets imported by several build functions) and attaches them via register_nodes(nav).
The MkNavs / Using decorators section is built this way.
Example
import mknodes as mk
nav = mk.MkNav("Guides")
@nav.route.page("First steps", icon="rocket")
def _(page: mk.MkPage):
page += "Some text"
@nav.route.nav("Advanced")
def _(nav: mk.MkNav):
nav.add_page("Plugins")
mknodes.Router
class Router
Class used for MkNav decorator routing.
mknodes.navs.navrouter.NavRouter
class NavRouter
Class used for MkNav decorator routing.
__call__
def __call__(*path: str) -> Callable[[AnyNavSubTypeCallable], Any]
Decorator method to use for routing.
The decorated functions need to return either a MkPage or an MkNav. They will get registered to the router-MkNav then.
In comparison to page() and nav(), it is not required to know in advance what
will be returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | The section path for the returned MkNav / MkPage | () |
__init__
def __init__(nav: mknav.MkNav) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nav | mknav.MkNav | MkNav to use for routing |
nav
def nav(*path: str, **kwargs: Any) -> Callable[[AnyNavCallable], Any]
Decorator method to use for routing Navs.
The decorated functions will get passed an MkNav as an argument which can be modified then. They will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkNav and return it in the decorated method, that MkNav takes preference then over the modified one.
Examples:
@nav.route.nav("Routed nav")
def _(nav: mk.MkNav):
nav += ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path | str | The section path for the returned MkNav | () |
kwargs | Any | Keyword arguments passed to the MkNav constructor. | {} |
page
def page(*path: str, **kwargs: Any) -> Callable[[AnyPageCallable], Any]
Decorator method to use for routing Pages.
The decorated functions will get passed an MkPage as an argument which can be modified then. The resulting page will get registered to the router-MkNav afterwards.
There is also the possibility to create a new MkPage and return it in the decorated method, that MkPage takes preference then over the modified one.
The keyword arguments can be used to pass metadata-related keyword arguments to the MkPage constructor.
Examples:
@nav.route.page("Routed page", icon=...)
def _(page: mk.MkPage):
page += ...
Args: path: The section path for the returned MkPage kwargs: Keyword arguments passed to the MkPage constructor.
Page info
Code for this page
mknodes.manual.api_section.add_page.
@nav.route.page(title, hide="toc")
def _(page: mk.MkPage) -> None:
page += INTROS[title]
page += mk.MkCode(EXAMPLES[title].rstrip(), language="py", title="Example")
for identifier in identifiers:
page += mk.MkDocStrings(identifier, **DOCSTRING_OPTIONS)
Resources
Resources(css=[],
markdown_extensions={},
plugins=[],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/api_section.py
source_function: _add_page.<locals>._
source_line_no: 168
hide:
- toc
title: Routing